package utli;

import java.sql.*;

public class DBUtil {

    private static final String URL = "jdbc:mysql://localhost:3306/mybattis";
    private static final String user = "root";
    private static final String password = "root";
    //静态封装随着类加载而执行 内容就存在一份
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //获得连接对象
    public static Connection getconn(){
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(URL,user,password);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }   
        return conn;
    }

    //关流
    public static void close (Connection conn, Statement s, ResultSet r){
        if (conn != null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (s !=null){
            try {
                s.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (r != null){
            try {
                r.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    public static void closeAll(Connection conn, Statement s) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace( );
            }
        }
        if (s != null) {
            try {
                s.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}


